package com.techshine.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.techshine.entity.*;
import com.techshine.mapper.*;
import com.techshine.service.ConstantService;
import com.techshine.util.JwtUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.techshine.common.base.BaseController;
import com.techshine.common.base.BaseEntity;
import com.techshine.util.StringUtil;

/** 
 * 说明：节点
 * 创建时间：2023-12-12
 */
@Controller 
@RequestMapping(value="/main")
public class MainController extends BaseController {
	@Resource
	private ConstantMapper constantMapper;
	@Resource
	private MyfileMapper myfileMapper;
	@Resource
	private NewsMapper newsMapper;
	@Resource
	private MenuMapper menuMapper;
	@Resource
	private EquMapper equMapper;

	@Resource
	private ConstantService constantService;

	@ResponseBody 
    @RequestMapping("/index")
    public Map<String, Object>  index() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Constant query7 = constantMapper.queryByKey("7");
		map.put("code", 0);
		Constant query8 = constantMapper.queryByKey("8");
		map.put("code", 0);
		map.put("footcont", query7.getVal());
		map.put("footflag", query8.getVal());
		return map;
    }
	/**
	 * 首页
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody 
    @RequestMapping("")
    public Map<String, Object>  main(String id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Myfile myfile = new Myfile();
		myfile.setFiletype(1);
		List<Myfile> imgList = myfileMapper.queryList(myfile);
		Equ equ = new Equ();
		equ.setCategory("2");
		equ.setLimit(10);
		List<Equ> img2List = equMapper.queryListByPage(equ);
		News news = new News();
		news.setLimit(6);
		news.setNewtype(1);
		news.setStatus(2);
		List<News> newsList =  newsMapper.queryListByPage(news);
		news.setNewtype(2);
		List<News> news2List =  newsMapper.queryListByPage(news);
		map.put("code", 0);
		map.put("imglist", imgList);
		map.put("img2list", img2List);
		map.put("newslist", newsList);
		map.put("news2List", news2List);
		return map;
    }
	/**
	 * 新闻与通知公告
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody 
    @RequestMapping("/news")
    public Map<String, Object>  news(String id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		 News queryByKey = newsMapper.queryByKey(id);
		 map.put("code", 0);
		 map.put("news", queryByKey);
		return map;
    }
	/**
	 * 新闻与通知公告列表
	 * @param news
	 * @return
	 * @throws Exception
	 */
	@ResponseBody 
    @RequestMapping("/newslist")
    public Map<String, Object>  newslist(News news) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		news.setStatus(2);
		 List<News> querylist = newsMapper.queryListByPage(news);
		 map.put("code", 0);
		 map.put("data", querylist);
		 map.put("count", news.getCount());
		return map;
    }
	/**
	 * 只有一个菜单并且直接显示菜单内容---章程
	 * @param news
	 * @return
	 * @throws Exception
	 */
	@ResponseBody 
    @RequestMapping("/querymenuone")
    public Map<String, Object>  querymenuone(Menu menu) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<BaseEntity> queryList = menuMapper.queryList(menu);
		if(queryList!=null&&queryList.size()>0) {
			 Menu queryByKey = menuMapper.queryByKey(queryList.get(0).getId()+"");
			 map.put("code", 0);
			 map.put("news", queryByKey);
		}else {
			 map.put("code", 0);
			 map.put("msg", "无章程");
		}
		return map;
    }
	/**
	 * 显示菜单列表   赛前培训，作品展示
	 * @param news
	 * @return
	 * @throws Exception
	 */
	@ResponseBody 
    @RequestMapping("/querymenulist")
    public Map<String, Object>  querymenulist(Menu menu) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<BaseEntity> queryList = menuMapper.queryList(menu);
		 map.put("code", 0);
		 map.put("data", queryList);
		return map;
    }
	/**
	 * 文件下载列表
	 * @param menutype
	 * @return
	 * @throws Exception
	 */
	@ResponseBody 
    @RequestMapping("/queryfilelist")
    public Map<String, Object>  queryfilelist(Myfile myfile) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Myfile> queryListByPage = myfileMapper.queryListByPage(myfile);
		map.put("code", 0);
		 map.put("data", queryListByPage);
		 map.put("count", myfile.getCount());
		return map;
    }
	/**
	 * 赛前培训
	 * @param menutype
	 * @return
	 * @throws Exception
	 */
	@ResponseBody 
    @RequestMapping("/querymenuById")
    public Map<String, Object>  querymenuById(String id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Menu menu = menuMapper.queryByKey(id);
		 map.put("code", 0);
		 map.put("data", menu);
		return map;
    }
	/**
	 * 作品展示列表
	 * @param menutype
	 * @return
	 * @throws Exception
	 */
    
	@ResponseBody 
    @RequestMapping("/queryimglist")
    public Map<String, Object>  queryimglist(Equ equ) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		equ.setLimit(8);
		List<Equ> queryList = equMapper.queryListByPage(equ);
		map.put("code", 0);
		 map.put("data", queryList);
		 map.put("count", equ.getCount());
		return map;
    }
	
    @RequestMapping("/tologin2")
    public ModelAndView  tologin2(HttpServletRequest request,String name,String pass) throws Exception {
    	ModelAndView mv = this.getModelAndView();
    	mv.setViewName("/WEB-INF/views/login.html");
		return mv;
    }
    @RequestMapping("/tologin3")
    public ModelAndView  tologin3(HttpServletRequest request,String name,String pass) throws Exception {
    	ModelAndView mv = this.getModelAndView();
    	mv.setViewName("/login.jsp");
		return mv;
    }
    @RequestMapping("/tologin4")
    public ModelAndView  tologin4(HttpServletRequest request,String name,String pass) throws Exception {
    	ModelAndView mv = this.getModelAndView();
    	mv.setViewName("login.jsp");
		return mv;
    }
   
    @RequestMapping("/tologin5")
    public ModelAndView  tologin5(HttpServletRequest request,String name,String pass) throws Exception {
    	ModelAndView mv = this.getModelAndView();
    	mv.setViewName("/back/login.html");
		return mv;
    }
	@ResponseBody 
    @RequestMapping("/login")
    public Map<String, Object>  login(HttpServletRequest request,String username,String password) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtil.isNotEmpty(username)&&StringUtil.isNotEmpty(password)) {
			if("admin".equals(username)) {
				//User queryByKey = mainMapper.queryByKey1(name);
				//制作token令牌

				User user=new User();
				user.setName(username);
				User queryByKey=constantService.queryByKeyqqq(user);
				Long roo= (long) queryByKey.getId();
				String token = JwtUtil.createAdminToken(roo);
				if(password.equals(queryByKey.getPass())) {
					request.getSession().setAttribute("currentUser",queryByKey);
					map.put("token",token);
				}else {
					map.put("code", 1);
					map.put("msg", "用户名或者密码错误");
					return map;
				}
			}else {
				map.put("code", 1);
				map.put("msg", "用户名或者密码错误");
				return map;
			}
		}else {
			map.put("code", 1);
			map.put("msg", "用户名或者密码为空");
			return map;
		}
		
		map.put("code", 3);
		return map;
    }
	@ResponseBody 
    @RequestMapping("/queryByKey")
    public Map<String, Object>  queryConstant(String id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		//map.put("result", mainMapper.queryConstant("1"));
		return map;
    }
	/**
	 * 权限鉴定:[mengiy][1234]
	 * m:menu 菜单
	 * e:equ   设备
	 * n:news 新闻
	 * g:msg  留言
	 * i:img  图片
	 * s:sys  系统
	 * 
	 * 0查1添加，2修改，3删除  0可以省略
	 * 如字符串m1m2e3g   表示对菜单有看加删权限，对设备有删权限，对留言有看权限
	 */
	
}
